package com.yogee.topseller.activity.my;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AppManager;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.login.view.impl.ForgetPswActivity;
import com.yogee.topseller.activity.login.view.impl.GuideLoginRegist;
import com.yogee.topseller.bean.BankInformationBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.view.BasicDialog;

import butterknife.BindView;
import butterknife.OnClick;

public class TakeCashActivity extends HttpActivity {

    @BindView(R.id.take_cash_title_layout)
    TitleLayout takeCashTitleLayout;
    @BindView(R.id.take_cash_card_num_tv)
    TextView takeCashCardNumTv;
    @BindView(R.id.take_cash_et)
    EditText takeCashEt;
    @BindView(R.id.take_cash_card_bank_tv)
    TextView bankTv;

    private String userid;
    private String money;
    private String cardId = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_take_cash;
    }
    
    @Override
    protected void initView() {
        takeCashTitleLayout.setActivity(this);
        takeCashTitleLayout.setTitle("提现");
        userid = AppUtil.getUserId(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bankInformation(AppUtil.getUserId(this), cardId);
    }

    @OnClick({R.id.take_cash_card_genghuan_tv, R.id.take_cash_card_tixian_tv, R.id.all_tixian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.take_cash_card_genghuan_tv://更换佣金提现账号
                startActivityForResult(new Intent(this, ChoseCardActivity.class), 111);
                break;
            case R.id.take_cash_card_tixian_tv://最下面的提现
                String moneys = takeCashEt.getText().toString();
                if (!cardId.isEmpty()) {
                    bankCase(cardId, userid, moneys);
                } else {
                    ToastUtils.showToast(this, "您还未选择银行卡");
                }
                break;
            case R.id.all_tixian://全部提现
                takeCashEt.setText(money);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111) {
            cardId = data.getStringExtra("cardId");
        }
    }


    private void bankCase(String cardId, String userId, String money) {
        HttpManager.getInstance().bankCase(cardId, userId, money).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    ToastUtils.showToast(TakeCashActivity.this, "提现成功");
//                    AppManager.finishActivity(MyCommissionActivity.class);
                    try{
                        AppManager.finishActivity(MyCommissionActivity.class);
                    }catch(Exception e){
                    }
                    finish();
                }
            }
        }, this));
    }

    private void bankInformation(String userid, String cardId) {
        HttpManager.getInstance().bankInformation(userid, cardId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<BankInformationBean>() {
            @Override
            public void onNext(BankInformationBean o) {
                initViewData(o);
                TakeCashActivity.this.cardId = o.getId();
            }
        }, this));
    }

    private void initViewData(BankInformationBean o) {
        takeCashCardNumTv.setText(o.getBank() + o.getBankNumber());
        bankTv.setText(o.getBackCase());
        money = o.getMoney();
    }

    @Override
    public void showMsg(String msg) {
        super.showMsg(msg);
        if (msg.equals("还没有绑定银行卡！")) {
            noCard();
        }
    }

    private void noCard() {
        BasicDialog.Builder builder = new BasicDialog.Builder(this);
        builder.setMessage("您还未绑定银行卡!~")
                .setPositiveButton("确定", new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(TakeCashActivity.this, AccountManagerActivity.class);
                        startActivity(intent);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("忽略", new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });

        builder.create().show();
    }
}
